﻿using Sidvall.IdentityManager.Data;
using System.Threading.Tasks;

namespace Sidvall.IdentityManager.UI.Entities
{
	public sealed partial class ServerItem
	{
		#region Public Members

		#region IsLoadedServer

		private bool _IsLoadedServer;
		public bool IsLoadedServer
		{
			get
			{
				return _IsLoadedServer;
			}
			set
			{
				if (_IsLoadedServer != value)
				{
					_IsLoadedServer = value;
					OnPropertyChanged("IsLoadedServer", true, false);
					OnPropertyChanged("IsLoaded", true, false);
					OnPropertyChanged("ServerComment", true, false);
				}
			}
		}

		#endregion
		#region StatusMessageServer

		private string _StatusMessageServer = string.Empty;
		public string StatusMessageServer
		{
			get
			{
				return _StatusMessageServer;
			}
			set
			{
				value = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
				if (value != _StatusMessageServer)
				{
					_StatusMessageServer = value;
					OnPropertyChanged("StatusMessageServer", true, false);
				}
			}
		}

		#endregion

		#region IsLoaded

		public bool IsLoaded
		{
			get
			{
				if (!this.IsLoadedServer)
					return false;
				return true;
			}
		}

		#endregion
		#region IsRemoteServer

		public bool IsRemoteServer
		{
			get
			{
				return ConnectionTypeManager.IsRemote(this.ConnectionTypeId);
			}
		}

		#endregion
		#region IsSqlServerServer

		public bool IsSqlServerServer
		{
			get
			{
				return ConnectionTypeManager.IsSqlServer(this.ConnectionTypeId);
			}
		}

		#endregion
		#region ServerComment

		public string ServerComment
		{
			get
			{
				if (!this.IsLoadedServer)
					return "Kunde inte ansluta till servern.";
				else
					return ConnectionTypeManager.GetConnectionDescription(this.ConnectionTypeId, this.RemoteAddress, this.ConnectionString);
			}
		}

		#endregion

		#region ClearCustomFields

		public void ClearCustomFields()
		{
			this.IsLoadedServer = false;
			this.StatusMessageServer = string.Empty;
			if (this.Plugins != null)
				this.Plugins.ClearCustomFields();
		}

		#endregion
		#region ImportCustomFields

		public void ImportCustomFields(ServerItem source)
		{
			if (source == null)
				return;
			this.IsLoadedServer = source.IsLoadedServer;
			this.StatusMessageServer = source.StatusMessageServer;
			if (this.Plugins != null)
				this.Plugins.ImportCustomFields(source.Plugins);
		}

		#endregion
		#region AddPluginAsync

		public async Task<Plugin> AddPluginAsync(string className)
		{
			Plugin item;

			item = new Plugin()
			{
				PluginId = Sidvall.IdentityManager.SystemContextConfig.Current.EntityFacade.ServerItemManager.GetFreePluginId(this),
				EditIsEnabled = true,
				FilterIsEnabled = true,
				TypeName = className,
			};
			await item.InitializeAsync(true);
			if (this.Plugins == null)
				this.Plugins = new PluginCollection();
			this.Plugins.Add(item);

			return item;
		}

		#endregion
		#region InitializePluginsAsync

		public async Task InitializePluginsAsync()
		{
			if (this.Plugins == null)
				return;
			await this.Plugins.InitializeAsync();
		}

		#endregion

		#region ToString

		public override string ToString()
		{
			return this.Name;
		}

		#endregion

		#endregion
	}
}